﻿using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;

namespace ManagementApi.Models.Manage.CheckIn
{
    public class CheckInDetailExportDto
    {
        public string 序号 { get; set; }
        public string 姓名 { get; set; }
        public string 所属项目 { get; set; }
        public string 所属公司 { get; set; }
        public string 公司类型 { get; set; }
        public string 班组 { get; set; }
        public string 工种 { get; set; }
        public string 考勤状态 { get; set; }
        public string 进出标识 { get; set; }
        public string 考勤日期 { get; set; }
        public string 考勤来源 { get; set; }
        public string 开始用工日期 { get; set; }
        public string 结束用工日期 { get; set; }

        public string 创建时间 { get; set; }
    }

    public static partial class CheckInDetailExportExceptions
    {
        /// <summary>
        /// 将数据库模型转换成导出excel模型
        /// </summary>
        /// <param name="details"></param>
        /// <param name="project"></param>
        /// <param name="applicationContext"></param>
        /// <returns></returns>
        public static IEnumerable<CheckInDetailExportDto> AsExportDtos(this IEnumerable<Modules.CheckIn.Entitys.CheckInDetail> details,ProjectInfo project,IApplicationContext applicationContext)
        {
            var projectGroupService = applicationContext.GetService<ProjectGroupService>();
            var projectGroups = projectGroupService.GetProjectGroups(details.Select(a => a.GroupId));

            var projectGroupMemberService = applicationContext.GetService<ProjectGroupMemberService>();
            var projectMembers = projectGroupMemberService.GetProjectGroupMembersByPersonIds(details.Select(a => a.PersonId));

            var personService = applicationContext.GetService<PersonService>();
            var persons = personService.GetPersons(details.Select(a => a.PersonId));

            var corpService = applicationContext.GetService<CorpService>();
            var corps = corpService.GetCorpsByProjectId(project.ProjectId);

            var projectCorpService = applicationContext.GetService<ProjectCorpService>();
            var projectCorps = projectCorpService.GetProjectCorpsByProjectId(project.ProjectId);

            List<CheckInDetailExportDto> dtos = new();
            foreach (var item in details)
            {
                CheckInDetailExportDto dto = new()
                {
                    姓名 = persons.FirstOrDefault(a => a.PersonId == item.PersonId)?.Name ?? string.Empty,
                    所属项目 =project.Name,
                    所属公司 = corps.FirstOrDefault(a => a.CorpId == projectGroups.FirstOrDefault(i => i.GroupId == item.GroupId)?.CorpId)?.Name ?? string.Empty,
                    公司类型 = "",
                    班组 = projectGroups.FirstOrDefault(a => a.GroupId == item.GroupId)?.Name ?? string.Empty,
                    工种 = projectMembers.FirstOrDefault(a => a.PersonId == item.PersonId)?.Type?.GetDescription() ?? string.Empty,
                    考勤状态 = item.Status.GetDescription(),
                    进出标识 = item.EntryExitMark.GetDescription(),
                    考勤日期 = item.CheckinDate.ToString("yyyy-MM-dd"),
                    考勤来源 = item.DataSources.GetDescription(),
                    开始用工日期 = projectGroups.FirstOrDefault(a => a.GroupId == item.GroupId)?.StartDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    结束用工日期 = projectGroups.FirstOrDefault(a => a.GroupId == item.GroupId)?.EndDate?.ToString("yyyy-MM-dd") ?? string.Empty,
                    创建时间 = item.CreateTime.ToString("yyyy-MM-dd"),
                };
                var corpId = projectGroups.FirstOrDefault(i => i.GroupId == item.GroupId)?.CorpId;
                if (corpId == project.LumpContractorCorpId)
                    dto.公司类型 = "总承包";
                else {
                    var projectCorp = projectCorps.FirstOrDefault(i => i.CorpId == corpId);
                    if (projectCorp != null)
                        dto.公司类型 = projectCorp.Role.GetDescription();
                }
                dtos.Add(dto);
            }
            return dtos;
        }
    }
}